package com.yungu.swift.admin.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.CRC32;

public class DataValidUtil {

    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }
        Map<String, Object> map = MapUtils.build();
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            if (null != field.get(obj)) {
                String value;
                if ("class java.util.Date".equals(field.getGenericType().toString())) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    value = sdf.format(field.get(obj));
                } else {
                    value = String.valueOf(field.get(obj));
                }
                if (StringUtils.isNotEmpty(value)) {
                    map.put(field.getName(), encryptCRC32(value.trim().getBytes("UTF-8")));
                }
            }
        }
        return map;
    }

    public static String check(ArrayNode jsonArray) {
        boolean checked = false;
        StringBuffer sb = new StringBuffer(" ");
        for (JsonNode json : jsonArray) {
            JsonNode checkJson = json.get("checkJson");
            JsonNode validJson = json.get("validJson");
            if (!checkJson.isNull() && !validJson.isNull()) {
                JsonNode msg = json.get("msg");
                //迭代器迭代 map集合所有的keys
                Iterator it = checkJson.iterator();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    if (checkJson.has(key) && validJson.has(key)) {
                        Long kcode = checkJson.get(key).asLong();
                        Long vcode = validJson.get(key).asLong();
                        if (null != kcode && null != vcode) {
                            if (!kcode.equals(vcode)) {
                                checked = true;
                                if (msg.has(key)) {
                                    sb.append(msg.get(key).asText() + "，");
                                }
                            }
                        }
                    }
                }
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        String msg = "";
        if (checked && StringUtils.isNotEmpty(sb.toString())) {
            msg = "数据被篡改（" + sb.toString() + "）";
        }
        return msg;
    }

    /**
     * CRC32加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static long encryptCRC32(byte[] data) throws Exception {
        CRC32 crc32 = new CRC32();
        crc32.update(data);
        return crc32.getValue();
    }
}
